/**
 * 
 */
package com.pmsco.pms.controller;

import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.pmsco.pms.annotation.LogonRequired;
import com.pmsco.pms.business.AdminBusiness;
import com.pmsco.pms.business.LogonBusiness;
import com.pmsco.pms.business.dto.AdminMainInfoRequestDTO;
import com.pmsco.pms.business.dto.AdminMainInfoReturnDTO;
import com.pmsco.pms.business.dto.LogonRequestDTO;
import com.pmsco.pms.business.dto.LogonReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.util.BundleUtil;
import com.pmsco.pms.util.PmsLogonRequired;

/**
 * @author Long Tran
 *
 */
@Controller
public class PmsLogonController extends AbstractPmsController {
	private static final Logger logger = LoggerFactory.getLogger(PmsLogonController.class);
	
	/**
	 * Logon Action
	 */
	@RequestMapping(value = PmsConstant.logon_Url)
	@LogonRequired(required = PmsLogonRequired.NONE)
	public String logon(@RequestParam("adminLogon") Boolean adminLogon, @RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession session, Locale locale, Model model) {
		logger.info("Begin Logon Action");
		LogonBusiness logonBusiness = new LogonBusiness();
		
		logger.debug("Build logon request dto");
		LogonRequestDTO logonRequestDTO = new LogonRequestDTO();
		logonRequestDTO.setUserName(userName);
		logonRequestDTO.setPassword(password);
		logonRequestDTO.setAdminLogon(adminLogon);
		logonRequestDTO.setLogonFailThreshold(Integer.valueOf(BundleUtil.getInstance().getKey(PmsConstant.numberLogonFailThreshold)));
		logonRequestDTO.setEmailSubject(BundleUtil.getInstance().getKey(PmsConstant.accountLockEmailSubjectKey));
		logonRequestDTO.setEmailBody(BundleUtil.getInstance().getKey(PmsConstant.accountLockEmailBodyKey));
		
		logger.debug("Call to logon business for do logon");
		LogonReturnDTO logonReturnDTO = logonBusiness.doLogon(logonRequestDTO);
		
		logger.debug("Analysis logon return dto");
		model.addAttribute("logonFail", logonReturnDTO.isLogonFail());
		model.addAttribute("userName", logonReturnDTO.getUserName());
		model.addAttribute("password", logonReturnDTO.getPassword());
		model.addAttribute("warningMessage", logonReturnDTO.isShowWarningMessage());
		model.addAttribute("lockMessage", logonReturnDTO.isShowLockMessage());
		
		if(logonReturnDTO.getPmsUserSession() != null)
			session.setAttribute(PmsConstant.loggedUserSession, logonReturnDTO.getPmsUserSession());
		
		logger.info("End Logon Action");
		return logonReturnDTO.getReturnView();
	}
	
	/**
	 * User Logout Page
	 */
	@RequestMapping(value = PmsConstant.userLogout_Url)
	@LogonRequired(required = PmsLogonRequired.NORMAL_USER)
	public String userLogout(Locale locale, Model model, HttpSession session) {
		logger.info("Begin User Logout Action");
		session.removeAttribute(PmsConstant.loggedUserSession);
		buildMenuList(session);
		logger.info("End User Logout Action");
		return PmsConstant.userLogout_View;
	}
	
	/**
	 * Admin Logout Page
	 */
	@RequestMapping(value = PmsConstant.adminLogout_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	public String adminLogout(Locale locale, Model model, HttpSession session) {
		logger.info("Begin Admin Logout Action");
		session.removeAttribute(PmsConstant.loggedUserSession);
		buildMenuList(session);
		logger.info("End Admin Logout Action");
		return PmsConstant.adminLogout_View;
	}
	
	/**
	 * Admin Main Page
	 */
	@RequestMapping(value = PmsConstant.adminMain_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	public ModelAndView adminMain(Locale locale, Model model, HttpSession session) {
		logger.info("Begin View Admin Main Page Action");
		AdminBusiness adminBusiness = new AdminBusiness();
		
		logger.debug("Build view admin main page request dto");
		AdminMainInfoRequestDTO adminMainInfoRequestDTO = new AdminMainInfoRequestDTO();
		
		logger.debug("Call to admin business for get system information");
		AdminMainInfoReturnDTO adminMainInfoReturnDTO = adminBusiness.loadAdminMainInfo(adminMainInfoRequestDTO);
		ModelAndView returnModelAndView = new ModelAndView(adminMainInfoReturnDTO.getReturnView(), "command", adminMainInfoReturnDTO.getPmsSysInformation());
		
		logger.info("End View Admin Main Page Action");
		buildMenuList(session);
		return returnModelAndView;
	}
	
	/**
	 * User Main Page
	 */
	@RequestMapping(value = PmsConstant.userMain_Url)
	@LogonRequired(required = PmsLogonRequired.NORMAL_USER)
	public String userMain(Locale locale, Model model, HttpSession session) {
		logger.info("User Main Action");
		buildMenuList(session);
		return PmsConstant.userMain_View;
	}
	
	/**
	 * Forgot Password Page
	 */
	@RequestMapping(value = PmsConstant.forgotPassword_Url)
	@LogonRequired(required = PmsLogonRequired.NORMAL_USER)
	public String forgotPassword(Locale locale, Model model) {
		logger.info("Forgot Password Action");
		return PmsConstant.forgotPassword_View;
	}
	
	/**
	 * Remind Username Page
	 */
	@RequestMapping(value = PmsConstant.remindUsername_Url)
	@LogonRequired(required = PmsLogonRequired.NORMAL_USER)
	public String remindUsername(Locale locale, Model model) {
		logger.info("Remind Username Action");
		return PmsConstant.remindUsername_View;
	}
	
	/**
	 * Do Remind Username Page
	 */
	@RequestMapping(value = PmsConstant.doRemindUsername_Url)
	@LogonRequired(required = PmsLogonRequired.NORMAL_USER)
	public String doRemindUsername(@RequestParam("emailAddr") String emailAddr, Locale locale, Model model) {
		logger.info("Begin Do Remind Username Action");
		LogonBusiness logonBusiness = new LogonBusiness();
		
		logger.debug("Build remind username request dto");
		LogonRequestDTO logonRequestDTO = new LogonRequestDTO();
		logonRequestDTO.setEmail(emailAddr);
		logonRequestDTO.setEmailSubject(BundleUtil.getInstance().getKey(PmsConstant.doRemindUsernameEmailSubjectKey));
		logonRequestDTO.setEmailBody(BundleUtil.getInstance().getKey(PmsConstant.doRemindUsernameEmailBodyKey));
		
		logger.debug("Call to logon business for remind username");
		LogonReturnDTO logonReturnDTO = logonBusiness.doRemindUserName(logonRequestDTO);
		
		logger.info("End Do Remind Username Action");
		return logonReturnDTO.getReturnView();
	}
	
	/**
	 * Do Reset Password Page
	 */
	@RequestMapping(value = PmsConstant.doResetPassword_Url)
	@LogonRequired(required = PmsLogonRequired.NORMAL_USER)
	public String doResetPassword(@RequestParam("emailAddr") String emailAddr, Locale locale, Model model) {
		logger.info("Begin Do Reset Password Action");
		LogonBusiness logonBusiness = new LogonBusiness();
		
		logger.debug("Build reset password request dto");
		LogonRequestDTO logonRequestDTO = new LogonRequestDTO();
		logonRequestDTO.setEmail(emailAddr);
		logonRequestDTO.setEmailSubject(BundleUtil.getInstance().getKey(PmsConstant.doResetPasswordEmailSubjectKey));
		logonRequestDTO.setEmailBody(BundleUtil.getInstance().getKey(PmsConstant.doResetPasswordEmailBodyKey));
		
		logger.debug("Call to logon business for remind username");
		LogonReturnDTO logonReturnDTO = logonBusiness.doResetPassword(logonRequestDTO);
		
		logger.info("End Do Remind Username Action");
		return logonReturnDTO.getReturnView();
	}
	
}
